/*
 * Copyright ©2015-2021 Jaemon. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.jaemon.spring.adorner.starter;

import io.gitee.jaemon.spring.adorner.AdornerConstants;
import io.gitee.jaemon.spring.adorner.SpringAdornerStarter;
import io.gitee.jaemon.spring.adorner.core.AdornerApplicationContext;
import io.gitee.jaemon.spring.adorner.demo.entity.Response;
import io.gitee.jaemon.spring.adorner.demo.controller.OrderController;
import io.gitee.jaemon.spring.adorner.demo.controller.UserController;
import io.gitee.jaemon.spring.adorner.support.AdornerBanner;
import io.gitee.jaemon.spring.adorner.support.DefaultIndexConvert;
import io.gitee.jaemon.spring.adorner.support.MarkdownApiShow;
import io.gitee.jaemon.spring.adorner.type.CommonParameter;
import io.gitee.jaemon.spring.adorner.type.DocScanner;
import io.gitee.jaemon.spring.adorner.type.LogLevel;
import io.gitee.jaemon.spring.adorner.util.AdornerUtils;
import sun.misc.Request;

import java.io.File;

/**
 * 自定义执行上下文示例启动类
 *
 * @author Jaemon
 * @since 1.0
 */
public class AdornerContextExecutorStarter {

    public static void main(String[] args) {
        // JSON报文体是否带注释
         System.setProperty(AdornerConstants.SPRING_ADORNER_JSON_HANDLER_NOTES, "false");

         System.setProperty(AdornerConstants.SPRING_ADORNER_TABLE_COLUMN_NAME, "字段名称");
         System.setProperty(AdornerConstants.SPRING_ADORNER_TABLE_COLUMN_TYPE, "字段类型");
         System.setProperty(AdornerConstants.SPRING_ADORNER_TABLE_COLUMN_MUST, "是否必填");
         System.setProperty(AdornerConstants.SPRING_ADORNER_TABLE_COLUMN_NOTES, "字段注释");

        // 指定API文档生成路径(包括文件名)
        String filePath = "C:\\Users\\answer\\Desktop\\adorner\\api\\adorner-api.md";
        // 待扫描的API接口包路径
        String[] packages = new String[]{
                "io.gitee.jaemon.spring.adorner.demo.controller"
        };
        // 注释扫描器
        DocScanner docScanner = DocScanner.of(
                "io.gitee.jaemon.spring.adorner",
                // 因为当前接口类定义在test包下，所以需要指定classpath为test所在路径
                System.getProperty("user.dir") + File.separator + "src\\test\\java"
        );
        // 是否记录日志到文件, 日志文件名统一: adorner.log
        String logpath = "C:\\Users\\answer\\Desktop\\adorner\\log";

        AdornerApplicationContext context =
                AdornerApplicationContext.builder()
                        // 设置项目接口地址前缀, 默认: http://localhost:8080/adorner
                        .url("http://localhost:8888/adorner")
                        // 接口分组展示
                        .group(true)
                        // 不使用简化输出, 会输出所有的接口地址以及请求方法
                        .simplify(false)
                        // 采用注释扫描器(解析指定包下类的类注释、方法注释、字段注释并使用)
                        .docScanner(docScanner)
                        // 只生成指定类接口, 优先级高于excludeClasses
                        .includeClasses(AdornerUtils.newArrayList(UserController.class))
                        // 排除指定类接口
                        .excludeClasses(AdornerUtils.newArrayList(OrderController.class))
                        // 设置请求/响应公共部分类
                        .commonParameter(CommonParameter.of(Request.class, Response.class))
                        // 设置API展示形式
                        .apiParamShow(MarkdownApiShow.JSON)
                        // 自定义索引转换器
                        .indexConvert(new DefaultIndexConvert())
                        // 是否记录日志到文件, 日志名称: adorner.log
                        .logpath(logpath)
                        // 设置Adorner日志输出级别
                        .logLevel(LogLevel.INFO)
                        // 自定义banner
                        .banner(new AdornerBanner())
                        // 指定api文档生成路径(包括文件名)以及待扫描的包路径
                        .build(filePath, packages);

        SpringAdornerStarter.generate(context);
    }

}